struct STAR {
  int adr;
  CRGB color;
  boolean up;
  boolean done;
  int fade;
};

STAR treedot[60];

float x1[15];

void inittree() {
  for (int i = 0; i < 60; i++) {
    treedot[i].up = true;
    treedot[i].done = true;
    treedot[i].fade = 0;
  }
  for (int i = 0; i < 15; i++) {
    x1[i] = 0.1;
  }
}

void tree() {
static int spark;

  for ( int i; i < 120; i++ ) {
    leds[i] = CRGB::Red;
    leds[i].fadeLightBy(252);   // そのドットの明るさを設定
  }
  
  for ( int j; j < 15; j++ ) {
    if ( treedot[j].done ) {
      if ( random(200) < 1 ) {
        treedot[j].done = false;
        treedot[j].adr = random(8) + j * 8;
        treedot[j].color = doublecolor(random(12));
        treedot[j].up = true;
        treedot[j].fade = 0;
      } else {
        if ( random(500) < 1 ) {
          spark = random(8) + j * 8;
          leds[spark] = CRGB::White;
        }
      }
    } else {
      if ( treedot[j].up ) {
        leds[treedot[j].adr] = treedot[j].color;
        leds[treedot[j].adr].fadeToBlackBy(255 - treedot[j].fade);
        treedot[j].fade += 5;
        if ( treedot[j].fade >= 255 ) {
          treedot[j].up = false;
          treedot[j].fade = 255;
        }
      } else {
        leds[treedot[j].adr] = treedot[j].color;
        leds[treedot[j].adr].fadeToBlackBy(255 - treedot[j].fade);
        treedot[j].fade -= 5;
        if ( treedot[j].fade <= 0 ) {
          treedot[j].up = true;
          treedot[j].fade = 0;
          treedot[j].done = true;
        }
      }
    }
  }

  FastLED.show();      // LEDに表示データを送信して表示を変更する
  
  delay(25);
}

void tree2() {
static int spark;

  for ( int i; i < 120; i++ ) {
    leds[i] = CRGB::Red;
    leds[i].fadeLightBy(252);   // そのドットの明るさを設定
  }
  
  for ( int j; j < 30; j++ ) {
    if ( treedot[j].done ) {
      if ( random(200) < 1 ) {
        treedot[j].done = false;
        treedot[j].adr = random(4) + j * 4;
        treedot[j].color = doublecolor(random(12));
        treedot[j].up = true;
        treedot[j].fade = 0;
      } else {
        if ( random(500) < 1 ) {
          spark = random(4) + j * 4;
          leds[spark] = CRGB::White;
        }
      }
    } else {
      if ( treedot[j].up ) {
        leds[treedot[j].adr] = treedot[j].color;
        leds[treedot[j].adr].fadeToBlackBy(255 - treedot[j].fade);
        treedot[j].fade += 5;
        if ( treedot[j].fade >= 255 ) {
          treedot[j].up = false;
          treedot[j].fade = 255;
        }
      } else {
        leds[treedot[j].adr] = treedot[j].color;
        leds[treedot[j].adr].fadeToBlackBy(255 - treedot[j].fade);
        treedot[j].fade -= 5;
        if ( treedot[j].fade <= 0 ) {
          treedot[j].up = true;
          treedot[j].fade = 0;
          treedot[j].done = true;
        }
      }
    }
  }

  FastLED.show();      // LEDに表示データを送信して表示を変更する
  
  delay(25);
}

void tree3() {
static int spark;

  for ( int i; i < 120; i++ ) {
    leds[i] = CRGB::Red;
    leds[i].fadeLightBy(252);   // そのドットの明るさを設定
  }
  
  for ( int j; j < 60; j++ ) {
    if ( treedot[j].done ) {
      if ( random(200) < 1 ) {
        treedot[j].done = false;
        treedot[j].adr = random(2) + j * 2;
        treedot[j].color = doublecolor(random(12));
        treedot[j].up = true;
        treedot[j].fade = 0;
      } else {
        if ( random(500) < 1 ) {
          spark = random(2) + j * 2;
          leds[spark] = CRGB::White;
        }
      }
    } else {
      if ( treedot[j].up ) {
        leds[treedot[j].adr] = treedot[j].color;
        leds[treedot[j].adr].fadeToBlackBy(255 - treedot[j].fade);
        treedot[j].fade += 5;
        if ( treedot[j].fade >= 255 ) {
          treedot[j].up = false;
          treedot[j].fade = 255;
        }
      } else {
        leds[treedot[j].adr] = treedot[j].color;
        leds[treedot[j].adr].fadeToBlackBy(255 - treedot[j].fade);
        treedot[j].fade -= 5;
        if ( treedot[j].fade <= 0 ) {
          treedot[j].up = true;
          treedot[j].fade = 0;
          treedot[j].done = true;
        }
      }
    }
  }

  FastLED.show();      // LEDに表示データを送信して表示を変更する
  
  delay(25);
}

void candletree() {
static int spark;

  for ( int i; i < 120; i++ ) {
    leds[i] = CRGB::Red;
    leds[i].fadeLightBy(252);   // そのドットの明るさを設定
  }
  
  for ( int j; j < 15; j++ ) {
    if ( treedot[j].done ) {
      if ( random(200) < 1 ) {
        treedot[j].done = false;
        treedot[j].adr = random(8) + j * 8;
        treedot[j].color = CRGB(100,255,0); // Fire color
        leds[treedot[j].adr] = treedot[j].color;
        leds[treedot[j].adr].fadeToBlackBy(255 - chaos(j));
      } else {
        if ( random(500) < 1 ) {
          spark = random(8) + j * 8;
          leds[spark] = CRGB::White;
        }
      }
    } else {
      leds[treedot[j].adr] = treedot[j].color;
      leds[treedot[j].adr].fadeToBlackBy(255 - chaos(j));
    }
  }

  FastLED.show();      // LEDに表示データを送信して表示を変更する
  
  delay(40);
}

int chaos(int i){
float x;
  // 1/f yuragi
  x = x1[i];
  if(x < 0.5){
    x = x + 2 * x * x;
  } 
  else {
    x = x - 2 * (1.0 - x) * (1.0 - x);
  }

  if(x < 0.08){
//  if(x < 0.005){
    x = (float)(random(20,80))/256;
//    x = (float)(random(256))/256;
  }
  if(x > 0.995){
//  if(x > 0.999){
    x = (float)(random(128,253))/256;
//    x = (float)(random(256))/256;
  }

  x1[i] = x;
  x = x * 245 + 10;

  return (unsigned int)x;
}

